package com.lyy.mapper;

import com.lyy.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Repository
public interface UserMapper {
    String list = "select u.user_id, u.username, u.password, u.real_name, u.nick_name, " +
            "u.gender, u.age, u.id_card, u.avatar_url, u.phone, u.user_info, " +
            "u.user_deleted, u.create_time, u.modify_time " +
            "from lesson.user u ";

    /**
     * 插入一条用户记录
     *
     * @param user 插入字段
     * @return 影响条目数
     */
    @Insert("insert into lesson.user ( " +
            "username, password, real_name, nick_name, gender, age, id_card, avatar_url, " +
            "phone, user_info, user_deleted, create_time, modify_time) " +
            "values ( " +
            "#{username}, #{password}, #{realName}, #{nickName}, #{gender}, #{age}, " +
            "#{idCard}, #{avatarUrl}, #{phone}, #{userInfo}, #{userDeleted}, " +
            "#{createTime}, #{modifyTime}) ")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    int insert(User user);

    /**
     * 按条件查询用户记录
     *
     * @param condition 查询条件
     * @return 查询失败时返回空List对象
     */
    @Select("<script>" + list +
            "<where> " +
            "<if test='userId != null'>and u.user_id = #{userId}</if> " +
            "<if test='username != null'>and u.username = #{username}</if> " +
            "<if test='password != null'>and u.password = #{password}</if> " +
            "<if test='realName != null'>and u.real_name = #{realName}</if> " +
            "<if test='nickName != null'>and u.nick_name = #{nickName}</if> " +
            "<if test='gender != null'>and u.gender = #{gender}</if> " +
            "<if test='age != null'>and u.age = #{age}</if> " +
            "<if test='idCard != null'>and u.id_card = #{idCard}</if> " +
            "<if test='avatarUrl != null'>and u.avatar_url = #{avatarUrl}</if> " +
            "<if test='phone != null'>and u.phone = #{phone}</if> " +
            "<if test='userInfo != null'>and u.user_info = #{userInfo}</if> " +
            "<if test='userDeleted != null'>and u.user_deleted = #{userDeleted}</if> " +
            "<if test='createTime != null'>and u.create_time = #{createTime}</if> " +
            "<if test='modifyTime != null'>and u.modify_time = #{modifyTime}</if> " +
            "</where> " +
            "</script> ")
    List<User> select(User condition);

    /**
     * 按条件修改用户记录
     *
     * @param user      修改字段
     * @param condition 修改条件
     * @return 影响条目数
     */
    @Update("<script>" +
            "update lesson.user " +
            "<set> " +
            "<if test='param1.userId != null'> user_id = #{param1.userId},</if> " +
            "<if test='param1.username != null'> username = #{param1.username},</if> " +
            "<if test='param1.password != null'> password = #{param1.password},</if> " +
            "<if test='param1.realName != null'> real_name = #{param1.realName},</if> " +
            "<if test='param1.nickName != null'> nick_name = #{param1.nickName},</if> " +
            "<if test='param1.gender != null'> gender = #{param1.gender},</if> " +
            "<if test='param1.phone != null'> phone = #{param1.phone},</if> " +
            "<if test='param1.age != null'> age = #{param1.age},</if> " +
            "<if test='param1.avatarUrl != null'> avatar_url = #{param1.avatarUrl},</if> " +
            "<if test='param1.idCard != null'> id_card = #{param1.idCard},</if> " +
            "<if test='param1.userInfo != null'> user_info = #{param1.userInfo},</if> " +
            "<if test='param1.userDeleted!= null'> user_deleted = #{param1.userDeleted},</if> " +
            "<if test='param1.createTime != null'> create_time = #{param1.createTime},</if> " +
            "<if test='param1.modifyTime != null'> modify_time = #{param1.modifyTime},</if> " +
            "</set> " +
            "<where> " +
            "<if test='param2.userId != null'>and user_id = #{param2.userId}</if> " +
            "<if test='param2.username != null'>and username = #{param2.username}</if> " +
            "<if test='param2.password != null'>and password = #{param2.password}</if> " +
            "<if test='param2.realName != null'>and real_name = #{param2.realName}</if> " +
            "<if test='param2.nickName != null'>and nick_name = #{param2.nickName}</if> " +
            "<if test='param2.gender != null'>and gender = #{param2.gender}</if> " +
            "<if test='param2.age != null'>and age = #{param2.age}</if> " +
            "<if test='param2.idCard != null'>and id_card = #{param2.idCard}</if> " +
            "<if test='param2.avatarUrl != null'>and avatar_url = #{param2.avatarUrl}</if> " +
            "<if test='param2.phone != null'>and phone = #{param2.phone}</if> " +
            "<if test='param2.userInfo != null'>and user_info = #{param2.userInfo}</if> " +
            "<if test='param2.userDeleted!=null'>and user_deleted = #{param2.userDeleted}</if>" +
            "<if test='param2.createTime != null'>and create_time = #{param2.createTime}</if> " +
            "<if test='param2.modifyTime != null'>and modify_time = #{param2.modifyTime}</if> " +
            "</where> " +
            "</script>")
    int update(User user, User condition);

    /**
     * 按条件删除用户记录
     *
     * @param condition 删除条件
     * @return 影响条目数
     */
    @Delete("<script>" +
            "delete from lesson.user " +
            "<where> " +
            "<if test='userId != null'>and user_id = #{userId}</if> " +
            "<if test='username != null'>and username = #{username}</if> " +
            "<if test='password != null'>and password = #{password}</if> " +
            "<if test='realName != null'>and real_name = #{realName}</if> " +
            "<if test='nickName != null'>and nick_name = #{nickName}</if> " +
            "<if test='gender != null'>and gender = #{gender}</if> " +
            "<if test='age != null'>and age = #{age}</if> " +
            "<if test='idCard != null'>and id_card = #{idCard}</if> " +
            "<if test='avatarUrl != null'>and avatar_url = #{avatarUrl}</if> " +
            "<if test='phone != null'>and phone = #{phone}</if> " +
            "<if test='userInfo != null'>and user_info = #{userInfo}</if> " +
            "<if test='userDeleted != null'>and user_deleted = #{userDeleted}</if> " +
            "<if test='createTime != null'>and create_time = #{createTime}</if> " +
            "<if test='modifyTime != null'>and modify_time = #{modifyTime}</if> " +
            "</where> " +
            "</script>")
    int delete(User condition);

    /**
     * 按用户表主键查询一条正常状态的用户记录（分步调用专用）
     *
     * @param userId 用户表主键
     * @return 查询失败时返回null
     */
    @Select(list + " where u.user_id = #{param1} and u.user_deleted = 0")
    User selectByUserId(Integer userId);




















    /*    *//**
     *
     *//*
    @Insert("insert into lesson.user (username,password,real_name,nick_name,gender,age,id_card,avatar_url,phone,user_info,user_deleted,create_time,modify_time) " +
            "value (#{username},#{password},#{realName},#{nickName},#{gender},#{age},#{idCard},#{avatarUrl},#{phone},#{userInfo},#{userDeleted},#{createTime},#{modifyTime})"
            )
    @Options(useGeneratedKeys = true,keyProperty = "userId")
    int insertUser(User user);
    @Select("<script>" +
            "select u.user_id, u.username, u.password, u.real_name, u.nick_name, " +
            "u.gender, u.age, u.id_card, u.avatar_url, u.phone, u.user_info, " +
            "u.user_deleted, u.create_time, u.modify_time " +
            "from lesson.user u" +
            "<where>" +
            "<if test='userId != null'>and user_id = #{userId}</if>" +
            "<if test='username != null'>and username = #{username}</if>" +
            "<if test='password != null'>and password = #{password}</if>" +
            "<if test='realName != null'>and real_name = #{realName}</if>" +
            "<if test='nickName != null'>and nick_name = #{nickName}</if>" +
            "<if test='gender != null'>and gender = #{gender}</if>" +
            "<if test='age != null'>and age = #{age}</if>" +
            "<if test='idCard != null'>and id_card = #{idCard}</if>" +
            "<if test='avatarUrl != null'>and avatar_url = #{avatarUrl}</if>" +
            "<if test='phone != null'>and phone = #{phone}</if>" +
            "<if test='userInfo != null'>and user_info = #{userInfo}</if>" +
            "<if test='createTime != null'>and create_time = #{createTime}</if>" +
            "<if test='modifyTime != null'>and modify_time = #{modifyTime}</if>" +
            " and user_deleted = 0" +
            "</where>" +
            "</script>")
    List<User> select(User user);

    *//**
     * 修改User记录
     *
     * @param field User实体，存放修改字段
     * @param condition User实体，存放修改条件
     * @return 影响条目数
     *//*
    @Update("<script>" +
            "update lesson.user" +
            "<set> " +
            "<if test='param1.username != null'> username = #{param1.username}, </if>" +
            "<if test='param1.password != null'> password = #{param1.password}, </if>" +
            "<if test='param1.realName != null'> real_name = #{param1.realName}, </if>" +
            "<if test='param1.nickName != null'> nick_name = #{param1.nickName}, </if>" +
            "<if test='param1.gender != null'> gender = #{param1.gender}, </if>" +
            "<if test='param1.phone != null'> phone = #{param1.phone}, </if>" +
            "<if test='param1.age != null'> age = #{param1.age}, </if>" +
            "<if test='param1.avatarUrl != null'> avatar_url = #{param1.avatarUrl}, </if>" +
            "<if test='param1.idCard != null'> id_card = #{param1.idCard}, </if>" +
            "<if test='param1.userInfo != null'> user_info = #{param1.userInfo}, </if>" +
            "<if test='param1.userDeleted != null'> user_deleted = #{param1.userDeleted}, </if>" +
            "<if test='param1.createTime != null'> create_time = #{param1.createTime}, </if>" +
            "<if test='param1.modifyTime != null'> modify_time = #{param1.modifyTime}, </if>" +
            "</set>" +
            "<where>" +
            "<if test='param2.userId != null'>and user_id = #{param2.userId}</if>" +
            "<if test='param2.username != null'>and username = #{param2.username}</if>" +
            "<if test='param2.password != null'>and password = #{param2.password}</if>" +
            "<if test='param2.realName != null'>and real_name = #{param2.realName}</if>" +
            "<if test='param2.nickName != null'>and nick_name = #{param2.nickName}</if>" +
            "<if test='param2.gender != null'>and gender = #{param2.gender}</if>" +
            "<if test='param2.age != null'>and age = #{param2.age}</if>" +
            "<if test='param2.idCard != null'>and id_card = #{param2.idCard}</if>" +
            "<if test='param2.avatarUrl != null'>and avatar_url = #{param2.avatarUrl}</if>" +
            "<if test='param2.phone != null'>and phone = #{param2.phone}</if>" +
            "<if test='param2.userInfo != null'>and user_info = #{param2.userInfo}</if>" +
            "<if test='param2.createTime != null'>and create_time = #{param2.createTime}</if>" +
            "<if test='param2.modifyTime != null'>and modify_time = #{param2.modifyTime}</if>" +
            " and user_deleted = 0" +
            "</where>" +
            "</script>")
    int update(User field, User condition);

    *//**
     * 删除User记录
     *
     * @param user User实体
     * @return 影响条目数
     *//*
    @Delete("<script>" +
            "delete from lesson.user " +
            "<where>" +
            "<if test='userId != null'>and user_id = #{userId}</if>" +
            "<if test='username != null'>and username = #{username}</if>" +
            "<if test='password != null'>and password = #{password}</if>" +
            "<if test='realName != null'>and real_name = #{realName}</if>" +
            "<if test='nickName != null'>and nick_name = #{nickName}</if>" +
            "<if test='gender != null'>and gender = #{gender}</if>" +
            "<if test='age != null'>and age = #{age}</if>" +
            "<if test='idCard != null'>and id_card = #{idCard}</if>" +
            "<if test='avatarUrl != null'>and avatar_url = #{avatarUrl}</if>" +
            "<if test='phone != null'>and phone = #{phone}</if>" +
            "<if test='userInfo != null'>and user_info = #{userInfo}</if>" +
            "<if test='userDeleted != null'>and user_deleted = #{userDeleted}</if>" +
            "<if test='createTime != null'>and create_time = #{createTime}</if>" +
            "<if test='modifyTime != null'>and modify_time = #{modifyTime}</if>" +
            "</where>" +
            "</script>")
    int delete(User user);


    @Delete("delete from lesson.user where user_id=#{userId}")
    int deleteUser(Integer userId);

    @Select("select * from lesson.user where user_id=#{param1} and user_deleted=0")
    User selectByUserId(Integer userId);

    *//*查询全部用户信息*//*
    @Select("select * from lesson.user where user_deleted =0 order by user_id")
    List<User>selectAllUser();

    String updateUser="<script> update lesson.user <set> " +
            "<if test='username!=null'>username=#{username},</if>" +
            "<if test='password!=null'>password=#{password},</if>" +
            "<if test='realName!=null'>real_name= #{realName},</if>" +
            "<if test='nickName!=null'>nick_name= #{nickName},</if>" +
            "<if test='gender!=null'>gender=#{gender},</if>" +
            "<if test='age!=null'>age=#{age},</if>" +
            "<if test='idCard!=null'>id_card=#{idCard},</if>" +
            "<if test='avatarUrl!=null'>avatar_url=#{avatarUrl},</if>" +
            "<if test='phone!=null'>phone=#{phone},</if>" +
            "<if test='userInfo!=null'>user_info=#{userInfo},</if>" +
            "<if test='userDeleted!=null'>user_deleted=#{userDeleted},</if>" +
            "<if test='createTime!=null'>create_time=#{createTime},</if>" +
            "<if test='modifyTime!=null'>modify_time=#{modifyTime},</if>" +
            "</set>" +
            " where user_id=#{userId} and user_deleted=0" +
            "</script>";
    @Update(updateUser)
    int updateByUserId(User user);

    *//*根据账号密码查询用户信息*//*
    @Select("select * from lesson.user where username=#{param1} and password=#{param2} and user_deleted=0")
    User selectByUsernameAndPassword(String username,String password);*/
}
